local skynet = require "skynet"
local sprotoloader = require "sprotoloader"
local cluster = require "skynet.cluster"
local runconfig = require "runconfig"

skynet.start(function()
	local mynode = skynet.getenv("node")
	local nodecfg = runconfig[mynode]

	local gate_host = skynet.getenv("gateserver_host")
	local gate_port = tonumber(skynet.getenv("gateserver_port"))
	local debug_port = tonumber(skynet.getenv("debug_console_port"))

	-- -- 集群
	-- cluster.reload(runconfig.cluster) 
	-- cluster.open(mynode) 
	-- skynet.newservice("redis", "redis")

	local protoloader = skynet.uniqueservice("protoloader")
	skynet.call(protoloader, "lua", "load", {
		"c2s.sproto", 
		"s2c.sproto",
	})
	
	if not skynet.getenv "daemon" then
		local console = skynet.newservice("console")
	end
	skynet.newservice("debug_console", debug_port)

	-- for i, v in pairs(nodecfg.watchdog) do 
	-- 	local srv = skynet.newservice("watchdog", "watchdog" .. i)
	-- 	skynet.call(srv, "lua", "start", {
	-- 		port = v.port, 
	-- 		maxclient = max_client,
	-- 		nodelay = true,
	-- 	})
	-- end 

	-- for i, v in pairs(nodecfg.login) do 
	-- 	local srv = skynet.newservice("login", "login" .. i)
	-- 	skynet.call(srv, "lua", "start")
	-- end 
	
	local logind = skynet.uniqueservice("login")
	local gated  = skynet.uniqueservice("gate") 

	local watchdog = skynet.uniqueservice("watchdog")
	
	skynet.call(watchdog, "lua", "start", {
		address = gate_host,
		port = gate_port,
		nodelay = true,
		maxclient = 1024,

		servername = "server1",

		loginservice = logind,
		gateservice = gated,
	})

	skynet.exit()
end)
